﻿using Ceri.Platform.EAM_NEW.API.Common;
using Ceri.Platform.EAM_NEW.API.IRepository.Base;
using Ceri.Platform.EAM_NEW.API.Repository.UnitOfWorks;
using SqlSugar;
using System;

namespace Ceri.Platform.EAM_NEW.API.Repository.Base
{
    public class BasicRepository : IBasicRepository
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly SqlSugarScope _dbBase;
        private ISqlSugarClient _db
        {
            get
            {
                var cons = AppSettings.Configuration[""];
                string sqlConnect = AppSettings.GetValue("DBS:Connection");
                ISqlSugarClient db = SqlSugarClient(sqlConnect);

                return db;
            }
        }

        public ISqlSugarClient Db => _db;

        public BasicRepository(IUnitOfWorkManage unitOfWorkManage)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _dbBase = unitOfWorkManage.GetDbClient();
        }

        public static SqlSugarClient SqlSugarClient(string connectionString)
        {
            ConnectionConfig config = new ConnectionConfig
            {
                ConnectionString = connectionString,
                DbType = DbType.Oracle,
                IsAutoCloseConnection = true
            };
            return new SqlSugarClient(config);
        }

        public int GetSequenceNextVal(string seqName)
        {
            string sqlstr = "select " + seqName + ".nextval from dual";
            var rlt = _dbBase.Ado.GetScalar(sqlstr);
            return Convert.ToInt32(rlt);
        }

    }
}
